
package com.hlkj.pay.util;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.pay.infrastructure.model.merchant.*;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.dto.merchant.MerchantAccessDto;
import com.hlkj.pay.dto.merchant.report.MerchantReportDto;
import com.hlkj.pay.infrastructure.model.payment.MerchantPaymentDO;
import com.hlkj.pay.infrastructure.model.payment.MerchantPaymentDetailDO;
import com.hlkj.pay.vo.openapi.merchant.resp.access.OpenApiMerchantAccessResp;
import com.hlkj.pay.vo.openapi.merchant.resp.report.OpenApiMerchantReportResp;
import com.hlkj.pay.vo.openapi.order.resp.PaymentApiResp;

/**
 * @author HlpayTeam
 * @date 2024/09/14 9:49
 */
public class OpenApiParamsUtils {

    public static PaymentApiResp paymentApiResp(MerchantPaymentDO merchantPaymentDO, MerchantPaymentDetailDO merchantPaymentDetailDO) {
        PaymentApiResp paymentApiResp = new PaymentApiResp();
        paymentApiResp.setMchChannelCode(merchantPaymentDO.getMchChannelCode());
        paymentApiResp.setEntryType(merchantPaymentDO.getEntryType());
        paymentApiResp.setMchOrderNo(merchantPaymentDetailDO.getMchOrderNo());
        paymentApiResp.setPayOrderNo(merchantPaymentDetailDO.getDetailPaymentId());
        paymentApiResp.setChannelOrderNo(merchantPaymentDetailDO.getThirdPaymentId());
        paymentApiResp.setInstOrderNo(merchantPaymentDetailDO.getInsOrderNo());
        paymentApiResp.setAmount(MerchantUtils.bigDecimalToString(merchantPaymentDetailDO.getPayAmount()));
        paymentApiResp.setClientIp(merchantPaymentDO.getClientIp());
        paymentApiResp.setRemark(merchantPaymentDO.getRemark());
        paymentApiResp.setMessage(merchantPaymentDetailDO.getRemark());
        paymentApiResp.setName(merchantPaymentDetailDO.getName());
        paymentApiResp.setCardNo(merchantPaymentDetailDO.getCardNo());
        if (StringUtils.hasText(merchantPaymentDO.getExtra())) {
            paymentApiResp.setExtra(merchantPaymentDO.getExtra());
        }
        paymentApiResp.setNotifyUrl(merchantPaymentDO.getNotifyUrl());
        paymentApiResp.setPayeeType(merchantPaymentDO.getPayeeType());
        paymentApiResp.setBankCode(merchantPaymentDetailDO.getBankCode());
        paymentApiResp.setBankName(merchantPaymentDetailDO.getBankName());
        paymentApiResp.setBranchCode(merchantPaymentDetailDO.getBranchCode());
        paymentApiResp.setSuccessTime(merchantPaymentDetailDO.getSuccessTime());
        paymentApiResp.setRefundSuccessTime(merchantPaymentDetailDO.getRefundSuccessTime());
        paymentApiResp.setStatus(merchantPaymentDetailDO.getStatus());
        return paymentApiResp;
    }

    public static OpenApiMerchantAccessResp openApiMerchantAccessResp(MerchantAccessDto merchantAccessDto) {
        MerchantAccessInfoDO merchantAccessInfo = merchantAccessDto.getMerchantAccessInfo();
        MerchantAccessCertInfoDO merchantAccessContact = merchantAccessDto.getMerchantAccessContact();
        MerchantAccessBusinessDO merchantAccessBusiness = merchantAccessDto.getMerchantAccessBusiness();
        OpenApiMerchantAccessResp openApiMerchantAccessResp = new OpenApiMerchantAccessResp();
        openApiMerchantAccessResp.setMchOrderNo(merchantAccessInfo.getMchOrderNo());
        openApiMerchantAccessResp.setProviderCode(merchantAccessInfo.getProviderCode());
        openApiMerchantAccessResp.setChannelCode(merchantAccessInfo.getChannelCode());
        openApiMerchantAccessResp.setName(merchantAccessInfo.getName());
        openApiMerchantAccessResp.setShortName(merchantAccessBusiness.getShortName());
        openApiMerchantAccessResp.setContactName(merchantAccessContact.getName());
        openApiMerchantAccessResp.setContactPhone(merchantAccessContact.getPhone());
        openApiMerchantAccessResp.setContactEmail(merchantAccessContact.getOtherValue());
        openApiMerchantAccessResp.setStatus(merchantAccessInfo.getStatus());
        openApiMerchantAccessResp.setSubMchId(merchantAccessInfo.getSubMchId());
        openApiMerchantAccessResp.setRequestId(merchantAccessInfo.getRequestId());
        openApiMerchantAccessResp.setMessage(merchantAccessInfo.getRemark());
        if (!CollectionUtils.isEmpty(merchantAccessBusiness.getSignLinks())) {
            openApiMerchantAccessResp.setSignLinks(JsonUtils.toJsonString(merchantAccessBusiness.getSignLinks()));
        }
        return openApiMerchantAccessResp;
    }

    public static OpenApiMerchantReportResp openApiMerchantReportResp(MerchantReportDto merchantReportDto) {
        MerchantReportInfoDO merchantReportInfo = merchantReportDto.getMerchantReportInfo();
        MerchantCertInfoDO merchantAccessContact = merchantReportDto.getMerchantReportContact();
        MerchantReportBusinessDO merchantAccessBusiness = merchantReportDto.getMerchantReportBusiness();
        OpenApiMerchantReportResp openApiMerchantAccessResp = new OpenApiMerchantReportResp();
        openApiMerchantAccessResp.setMchRequestId(merchantReportInfo.getMchRequestId());
        openApiMerchantAccessResp.setProviderCode(merchantReportInfo.getProviderCode());
        openApiMerchantAccessResp.setChannelCode(merchantReportInfo.getChannelCode());
        openApiMerchantAccessResp.setName(merchantReportInfo.getName());
        openApiMerchantAccessResp.setShortName(merchantAccessBusiness.getShortName());
        openApiMerchantAccessResp.setContactName(merchantAccessContact.getName());
        openApiMerchantAccessResp.setContactPhone(merchantAccessContact.getPhone());
        openApiMerchantAccessResp.setContactEmail(merchantAccessContact.getOtherValue());
        openApiMerchantAccessResp.setStatus(merchantReportInfo.getStatus());
        openApiMerchantAccessResp.setSubMchId(merchantReportInfo.getSubMchId());
        openApiMerchantAccessResp.setRequestId(merchantReportInfo.getRequestId());
        openApiMerchantAccessResp.setMessage(merchantReportInfo.getMessage());
        openApiMerchantAccessResp.setSignLink(merchantReportInfo.getSignLink());
        return openApiMerchantAccessResp;
    }

}
